<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h2>localStorage</h2>
    <button onclick="onSave()">点我保存数据</button>
    <button onclick="onRead()">点我读取数据</button>
    <button onclick="onDelete()">点我删除数据</button>
    <button onclick="onClear()">点我清除数据</button>
    <script type="text/javascript">

        function onSave(){

            // window.localStorage
            // 必须要求key 和 value都是字符串
            // 即便你写的是非字符，也会转化为字符串。默认调用
            // toString
            localStorage.setItem('item','hello')

            localStorage.setItem('item1',JSON.stringify({"name":'1123'}))
        }

        function onRead(){
            console.log(localStorage.getItem('item'),
            JSON.parse(localStorage.getItem('item2')));
        }

        function onDelete(){
            localStorage.removeItem('item')
        }

        function onClear(){
            localStorage.clear()
        }


    </script>
</body>
</html>